package org.example.rowing.service;

import lombok.extern.slf4j.Slf4j;
import org.example.rowing.domain.RoundScore;
import org.example.rowing.domain.Team;
import org.example.rowing.vo.RegistrationVO;
import org.example.rowing.vo.RoundScoreVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class OverDrawLotteryService {

    @Autowired
    private RaceInfoService raceInfoService;

    public Map<RegistrationVO, String> getLotteryResult(Long raceId, int teamCount) {
        Map<RegistrationVO, String> lotteryResult = new HashMap<>();
        RoundScoreVO roundScoreVOCondition = new RoundScoreVO();
        roundScoreVOCondition.setRaceId(raceId);
        roundScoreVOCondition.setRound("预赛");
        if (teamCount >= 49) {
            roundScoreVOCondition.setRound("计时赛");
        }
        List<RoundScoreVO> roundScoreVOS = raceInfoService.showRoundScoreByCondition(roundScoreVOCondition);
        log.info("roundScoreVOS {}",roundScoreVOS);
        for (RoundScoreVO roundScoreVO : roundScoreVOS) {
            lotteryResult.put(roundScoreVO.getRegistrationVO(), roundScoreVO.getLane());
        }
        return lotteryResult;
    }

    public void saveLotteryResult(Map<RegistrationVO, String> laneAssignments, Long raceId, int teamCount) {
        String round;
        if (teamCount >= 49) {
            round = "计时赛";
        } else {
            round = "预赛";
        }
        for (Map.Entry<RegistrationVO, String> entry : laneAssignments.entrySet()) {
            RegistrationVO registrationVO = entry.getKey();
            String lane = entry.getValue();
            RoundScore roundScore = new RoundScore(raceId, round, null, lane, null, registrationVO.getRegistrationId(), "NORMAL");
            raceInfoService.saveRoundScore(roundScore);
        }
    }
}
